import QtQuick 2.0
import Qt3D 1.0
import Qt3D.Shapes 1.0
import "Control"
import "js/BlockInfo.js" as BI

Viewport {
    id: _root
    width: 640
    height: 480

    focus: true
    navigation: false
//    picking: true

    property real temp: 0

    property int rotateCount: 0

    camera: Camera {
        center: Qt.vector3d(0, 0, 0)
        eye: Qt.vector3d(0, 0, 25)
        fieldOfView: 0
    }
    light: Light {
//        ambientColor: Qt.rgba(1, 1, 1, 1)
//        diffuseColor: Qt.rgba(0.5, 0.5, 0.5, 1)
//        specularColor: Qt.rgba(0.5, 0.5, 0.5, 1)
    }

    //崩しのタイマー
    Timer{
        id: _timer
        interval: 50
        repeat: false
        running: false

        property int breakCounter: 0
        property variant breakObj: undefined

        onTriggered: {
            if(breakCounter < BI.BC && breakObj != undefined){
                var p = Math.floor(Math.random() * 6);
                var r = Math.floor(Math.random() * 2);
                breakObj.rotateBlock(p, (r == 0) ? true : false);
                breakCounter += 1;
            }else if(breakCounter == BI.BC){
                _scene.state = "game";
                breakCounter = 0;
            }else{
                breakCounter = 0;
            }
        }

        function startBreak(){
            if(_titleView.textureMode == 0){
                breakObj = _blockView;
            }else{
                breakObj = _blockView1;
            }
            start();
        }
    }

    //キューブアニメ後の動作
    function afterRotated(obj){
        switch(_scene.state){
        case "game":
            //
            _root.rotateCount += 1;
            console.debug(" obj:" + obj.textureMode + obj.checkBlocksPosition());
            break;
        case "break":
            //崩す
            _timer.startBreak();
            break;
        default:
            break;
        }

    }

    //キューブ
    BlockView{
        id: _blockView
        mode: 1
        textureMode: 0

        onRunnningChanged: {
            if(runnning == false){
                afterRotated(_blockView);
            }
        }
    }
    //のろいモードキューブ
    BlockView{
        id: _blockView1
        mode: 1
        textureMode: 2

        onRunnningChanged: {
            if(runnning == false){
                afterRotated(_blockView1);
            }
        }
    }

    //タイトルロゴ
    TitleView{
        id: _titleView
        enabled: true
        textureMode: 0

        onTextureModeChanged: {
            updateEnabled();
        }

        onStartGame: {
            updateEnabled();
            //ゲーム開始
            _root.rotateCount = 0;
//            _scene.state = "game";
            _scene.state = "break";
        }

        function updateEnabled(){
            if(textureMode == 0){
                _blockView.enabled = true;
                _blockView1.enabled = false;
            }else{
                _blockView.enabled = false;
                _blockView1.enabled = true;
            }
        }
    }

    //回した回数
    NumberString{
        x: 5.2
        y: 4.4
        z: 1
        scale: 0.8
        number: _root.rotateCount
    }

    ControlPanel{
        pos_x: _timer.breakCounter

        onClicked: {
            switch(id){
            case 10:
                //North
                _blockView.rotateBlock(2);
                break;
            case 11:
                //West
                _blockView.rotateBlock(4);
                break;
            case 12:
                //South
                _blockView.rotateBlock(3);
                break;
            case 13:
                //East
                _blockView.rotateBlock(5);
                break;
            case 20:
                //Top
                _blockView.rotateBlock(0);
                break;
            case 21:
                //Bottom
                _blockView.rotateBlock(1);
                break;
            case 30:
                //L
                _scene.state = "";
//                _blocks.mode = 1;
                break;
            case 31:
                //G
                _scene.state = "game";
//                _blocks.mode = 0;
                break;
            case 32:
                //chk
//                _titleView.textureMode = 1;
//                var pos = Qt.vector3d(0, 0, 0);
//                for(var i=0; i<BI.block_position.length; i++){
//                    pos = BI.block_position[i].realPos();
//                    console.debug(BI.block_position[i].color + "    " + pos.x + "   " + pos.y + "   " + pos.z);
//                }
                break;
            default:
                break;
            }
        }

    }

    Keys.onPressed: {
        console.debug("KEY:" + event.key + "/" + event.modifiers + "/" + event.accepted);

        switch(_scene.state){
        case "game":
            if(_titleView.textureMode == 0){
                _blockView.keyEvent(event);
            }else{
                _blockView1.keyEvent(event);
            }
            break;
        case "break":
            break;
        default:
            _titleView.keyEvent(event);
            break;
        }

        event.accepted = true;

    }

    StateGroup{
        id: _scene
        states: [
            State{
                name: "game"
                PropertyChanges { target: _blockView; mode: 0 }
                PropertyChanges { target: _blockView1; mode: 0 }
                PropertyChanges { target: _titleView; enabled: false; }
            },
            State{
                name: "break"
                PropertyChanges { target: _blockView; mode: 0 }
                PropertyChanges { target: _blockView1; mode: 0 }
                PropertyChanges { target: _titleView; enabled: false; }
            }
        ]
    }

//    StateGroup{
//        id: _textureMode
//        states: [
//            State{
//                when: _titleView.textureMode == 1
//                PropertyChanges {
//                    target: _blockView
//                    enabled: false
//                }
//                PropertyChanges {
//                    target: _blockView1
//                    enabled: true
//                }
//            }

//        ]
//    }
}
